﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Liang.Util
{
    public class FileAndPath
    {
        /// <summary>
        /// 在应用程序目录下,创建一个按当前时间命名的文件夹
        /// </summary>
        /// <returns></returns>
        public static string MakePath()
        {
            return MakePath(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
        }

        /// <summary>
        /// 创建指定路径的文件夹
        /// </summary>
        /// <param name="strRoot"></param>
        /// <param name="bAddTimeFolder">是否在指定路径下，创建一个以当前时间命名的文件夹</param>
        /// <returns></returns>
        public static string MakePath(string strRoot, bool bAddTimeFolder = true)
        {
            string strPathRoot = string.Empty;
            string strTmp = MakeSureIsPath(strRoot);

            if (bAddTimeFolder)
            {
                strPathRoot = string.Format("{0}{1}{2:D2}{3:D2}\\",
                strTmp,
                DateTime.Now.Year,
                DateTime.Now.Month,
                DateTime.Now.Day);
            }
            else
            {
                strPathRoot = strTmp;
            }

            if (!Directory.Exists(strPathRoot))
            {
                Directory.CreateDirectory(strPathRoot);
            }

            return strPathRoot;
        }

        /// <summary>
        /// 获取指定路径，指定扩展名的所有文件
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="strExtension"></param>
        /// <returns></returns>
        public static List<string> GetFiles(string strPath, string strExtension = "*")
        {
            List<string> ret = new List<string>();

            DirectoryInfo dir = new DirectoryInfo(strPath);

            string strTmpExt = strExtension;
            if ("*" != strExtension)
            {
                strExtension.Trim(new char[] { '.' });
                strTmpExt = string.Format(".{0}", strExtension);
            }

            if (dir.Exists)
            {
                FileInfo[] childs = dir.GetFiles();
                foreach (FileInfo child in childs)
                {
                    if ("*" != strTmpExt)
                    {
                        if (strTmpExt == child.Extension)
                        {
                            ret.Add(child.FullName);
                        }
                    }
                    else
                    {
                        ret.Add(child.FullName);
                    }
                }
            }

            return ret;
        }

        /// <summary>
        /// 删除指定目录，指定扩展名的所有文件
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="strExtension"></param>
        /// <returns></returns>
        public static int DeleteFiles(string strPath, string strExtension = "*")
        {
            int nDeleted = 0;
            DirectoryInfo dir = new DirectoryInfo(strPath);

            string strTmpExt = strExtension;
            if ("*" != strExtension)
            {
                strExtension.Trim(new char[] { '.' });
                strTmpExt = string.Format(".{0}", strExtension);
            }

            if (dir.Exists)
            {
                FileInfo[] childs = dir.GetFiles();
                foreach (FileInfo child in childs)
                {
                    if ("*" != strTmpExt)
                    {
                        if (strTmpExt == child.Extension)
                        {
                            child.Delete();
                            nDeleted++;
                        }
                    }
                    else
                    {
                        child.Delete();
                        nDeleted++;
                    }
                }
            }

            return nDeleted;
        }

        /// <summary>
        /// 确保输入是文件夹路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string MakeSureIsPath(string path)
        {
            if ("\\" != path.Substring(path.Length - 1))
            {
                return path + "\\";
            }

            return path;
        }

        /// <summary>
        /// 读取文件到流
        /// </summary>
        /// <param name="strFile"></param>
        /// <returns></returns>
        public static byte[] ReadFileToStream(string strFile)
        {
            byte[] ret = null;

            try
            {
                int numBytesToRead = 0;
                int numBytesRead = 0;

                using (System.IO.FileStream fs = System.IO.File.OpenRead(strFile))
                {
                    ret = new byte[fs.Length];
                    numBytesToRead = (int)fs.Length;

                    while (numBytesToRead > 0)
                    {
                        int n = fs.Read(ret, numBytesRead, numBytesToRead);

                        if (n == 0)
                            break;

                        numBytesRead += n;
                        numBytesToRead -= n;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return ret;
        }

        /// <summary>
        /// 从流数据保存文件
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="strFileName"></param>
        public static void SaveFileFromStream(byte[] buffer, string strFileName)
        {
            try
            {
                using (FileStream fsNew = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    fsNew.Write(buffer, 0, buffer.Length);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
